package com.example.gulimall.product.app.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.vo.BrandResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.CategoryBrandRelationEntity;
import com.example.gulimall.product.service.CategoryBrandRelationService;
import com.example.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author lizhichao
 * @email sunlightcs@gmail.com
 * @date 2021-08-15 16:09:01
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * /product/categorybrandrelation/brands/list
     * value = "catId",required = true 表示必须传递
     */

    @GetMapping("/brands/list")
    public R brandList(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandEntityList= categoryBrandRelationService.getBrandByCatId(catId);

        List<BrandResVo> list=new ArrayList<>();
        for ( BrandEntity item: brandEntityList) {
            BrandResVo model=new BrandResVo();
            model.setBrandId(item.getBrandId());
            model.setBrandName(item.getName());
            list.add(model);
        }
        return R.ok().put("data",list);
    }
    /**
     * 列表
     */
    @GetMapping("/catelog/list")
   // @RequiresPermissions("product:categorybrandrelation:list")
    public R cateloglist(Long brandId){

        List<CategoryBrandRelationEntity> data=categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id",brandId));

        return R.ok().put("data", data
        );
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     * 电商系统大表数据不做关联
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
